package com.knowledge.controller;


import com.knowledge.common.aop.TokenValidate;
import com.knowledge.common.enums.ErrorCode;
import com.knowledge.dao.entity.FriendApplyEntity;
import com.knowledge.dao.entity.SysUser;
import com.knowledge.service.FriendApplyService;
import com.knowledge.service.FriendService;
import com.knowledge.utils.UserThreadLocal;
import com.knowledge.vo.Result;
import com.knowledge.vo.params.FriendParam;
import com.knowledge.vo.params.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * <p>
 * 好友表（插入原则：user_id < friend_user_id） 前端控制器
 * </p>
 *
 * @author zzz
 * @since 2022-03-03
 */
@RestController
@RequestMapping("/friend")
public class FriendController {

    @Autowired
    private FriendApplyService friendApplyService;

    @Autowired
    private FriendService friendService;

     /**
     　* @description: 好友列表
     　* @param ${tags}
     　* @return ${return_type}
     　* @throws
     　* @author zzz
     　* @date 2022-03-05 21:30
     */
    @TokenValidate
    @PostMapping("/list")
    public Result list(@RequestBody PageParams pageParams){
        SysUser sysUser = UserThreadLocal.get();
        return friendService.listFriendWithPage(sysUser, pageParams);
    }

    @TokenValidate
    @PostMapping("/applyList")
    public Result applyList(@RequestBody PageParams pageParams){
        SysUser sysUser = UserThreadLocal.get();
        return friendApplyService.listFriendApplyWithPage(sysUser.getId(), pageParams);
    }

     /**
     　* @description: 好友申请
     　* @param ${tags}
     　* @return ${return_type}
     　* @throws
     　* @author zzz
     　* @date 2022-03-05 11:19
     */
    @TokenValidate
    @PostMapping("/apply")
    public Result apply(@RequestBody Map<String, Object> params){
        if (params.get("friendUserId") == null){
            return Result.fail(ErrorCode.PARAMS_ERROR);
        }
        Long friendUserId = Long.parseLong(params.get("friendUserId").toString());
        SysUser sysUser = UserThreadLocal.get();
        if (friendUserId.equals(sysUser.getId())){
            return Result.fail(ErrorCode.APPLY_ADD_FRIEND_ERROR);
        }
        FriendParam friendParam = new FriendParam();
        friendParam.setFriendUserId(friendUserId);
        friendParam.setUserId(sysUser.getId());
        friendParam.setUserName(sysUser.getNickname());
        friendApplyService.applyFriend(friendParam);
        return Result.success();
    }

     /**
     　* @description: 同意好友
     　* @param ${tags}
     　* @return ${return_type}
     　* @throws
     　* @author zzz
     　* @date 2022-03-05 21:11
     */
    @TokenValidate
    @PostMapping("/agree")
    public Result agree(@RequestBody Map<String, Object> params){
        if (params.get("applyId") == null){
            return Result.fail(ErrorCode.PARAMS_ERROR);
        }
        SysUser sysUser = UserThreadLocal.get();
        Integer applyId = Integer.parseInt(params.get("applyId").toString());
        //查询申请校验是否自己的
        FriendApplyEntity friendApplyEntity = friendApplyService.getById(applyId);
        if (sysUser.getId().equals(friendApplyEntity.getCoverUserId())){
            friendApplyService.agreeFriendApply(applyId);
            return Result.success();
        }else {
            return Result.fail(ErrorCode.AGREE_FRIEND_APPLY_ERROR);
        }
    }

     /**
     　* @description: 好友搜索
     　* @param ${tags}
     　* @return ${return_type}
     　* @throws
     　* @author zzz
     　* @date 2022-03-05 21:12
     */
    @TokenValidate
    @PostMapping("/search")
    public Result search(@RequestBody Map<String, Object> params){
        if (params.get("account") == null){
            return Result.fail(ErrorCode.PARAMS_ERROR);
        }
        SysUser sysUser = UserThreadLocal.get();
        String account = params.get("account").toString();
        return friendService.searchFriend(sysUser.getId(), account);
    }

     /**
     　* @description: 解除好友
     　* @param ${tags}
     　* @return ${return_type}
     　* @throws
     　* @author zzz
     　* @date 2022-03-05 22:54
     */
    @TokenValidate
    @PostMapping("/relieve")
    public Result relieve(@RequestBody Map<String, Object> params){
        if (params.get("friendUserId") == null){
            return Result.fail(ErrorCode.PARAMS_ERROR);
        }
        SysUser sysUser = UserThreadLocal.get();
        Long friendUserId = Long.parseLong(params.get("friendUserId").toString());
        friendService.deleteFriend(sysUser.getId(), friendUserId);
        return Result.success();
    }
    
}
